using System;
using Godot;

public class FarmlandBlock : BlockMaterial
{
    public FarmlandBlock() : base("farmland")
    {
        SetDisplayName("耕地");
        SetStateMax(2);
        SetTickEvent((UpdataTickEvent e) =>
        {
            int range = 4;
            bool has = false;
            for (int i = -range; i < range; i++)
            {
                BlockData block = e.world.GetBlock(e.Position + new Vector3I(i, 0));
                if (block != null && block.Name == "water")
                {
                    has = true;
                    break;
                }
            }
            if (has)
            {
                if (e.block.State == 0) e.block.SetState(1);
            }
            else
            {
                if (e.block.State == 1) e.block.SetState(0);
                else
                {
                    if (new Random().Next(1000) == 1)
                    {
                        e.block.SetBlock(Materials.Dirt);
                    }
                }
            }
        });
    }
}